package kr.ac.kaist.ysproject;

import java.util.ArrayList;

import kr.ac.kaist.ysproject.data.DataSmartMsgInfo;
import kr.ac.kaist.ysproject.util.DbManager;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SmartMessageListActivity extends Activity {
	private ArrayList<DataSmartMsgInfo> m_arrSmartMsgInfoList;
	private SmartMessageListAdapter m_adapter;
	private ListView m_lvSmartMsgInfoList;
	private String	m_profileId = "";
	private String	m_profileName = "";
	private String	m_picturePath = "";

	@Override
	public void onStart(){
		super.onStart();
		//Toast.makeText(SmartMessageListActivity.this, "onStart", Toast.LENGTH_SHORT).show();

		DbManager dbm = DbManager.getInstance(SmartMessageListActivity.this);
		ArrayList<DataSmartMsgInfo> msgInfo = dbm.getSmartMsgInfo(m_profileId);

		m_lvSmartMsgInfoList = (ListView)findViewById(R.id.smart_message_list_view);
		m_arrSmartMsgInfoList = new ArrayList<DataSmartMsgInfo>();
		m_adapter = new SmartMessageListAdapter(this, R.layout.smart_message_list_row, m_arrSmartMsgInfoList);
		m_lvSmartMsgInfoList.setAdapter(m_adapter);

		for (int i = 0; i < msgInfo.size(); i++) {
			m_arrSmartMsgInfoList.add(msgInfo.get(i));
		}

		m_adapter.notifyDataSetChanged();

		m_lvSmartMsgInfoList.setOnItemClickListener(new OnItemClickListener(){
			public void onItemClick(AdapterView parent, View view, int position, long id){
				Log.d("select", Integer.toString(position));
				Intent intent = new Intent(SmartMessageListActivity.this, SmartMessageSettingActivity.class);
				intent.putExtra("ProfileId", m_profileId);
				intent.putExtra("ObjectData", m_arrSmartMsgInfoList.get(position));
				startActivity(intent);
			}
		}); 
	}

	@Override
	public void onResume(){
		super.onResume();
		//Toast.makeText(SmartMessageListActivity.this, "onResume", Toast.LENGTH_SHORT).show();
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.smart_message_list_layout);

		Intent intent = getIntent();

		if (intent != null) {
			m_profileId = intent.getStringExtra("ProfileId");
			m_profileName = intent.getStringExtra("ProfileName");
			m_picturePath = intent.getStringExtra("PicturePath");
		} 
		
		if (!m_picturePath.equals("")) {
			Bitmap bMap = BitmapFactory.decodeFile(m_picturePath);
			ImageView image = (ImageView) findViewById(R.id.iv_picture);
			image.setImageBitmap(bMap);
		}

		TextView tvProfileName = (TextView)findViewById(R.id.tv_profilename);
		tvProfileName.setText(m_profileName);

		Button btnAdd = (Button)findViewById(R.id.smart_message_list_add_button);

		btnAdd.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(SmartMessageListActivity.this, SmartMessageSettingActivity.class);
				intent.putExtra("ProfileId", m_profileId);
				startActivity(intent);
			}
		});

	}
}